﻿--#include "data\config\culture\CultureConfig.lua" once


---------------------------------------------世界等级更新 start----------------------------------------------

function updateWorldCultureLevel()
	local openDay = System.getDaysSinceOpenServer()
	local combineDay = System.getDaysSinceCombineServer()
	local minWorldLevel = WordCultureConfig.minWorldLevel
	local minActorLevel = WordCultureConfig.minActorLevel
	local minActorNum = WordCultureConfig.minActorNum


	local curWorldLevel = System.getCultureLevel()
	--print("curWorldLevel="..curWorldLevel)

	if openDay < WordCultureConfig.OpenServerDay then
		--print("openDay = "..openDay)
		return
	else 
		if combineDay ~= 0 and combineDay < WordCultureConfig.CombineServerDay then
			return
		end
	end

	local minLevel = minWorldLevel
	local list = LuaHelp.getActorLevelMaxTenPlayer()
	local len = 0
	if list then
		len = #list
	end
	if len ~= 0 then
		if len > minActorNum then
			len = minActorNum
		end
		local totalValue = 0
		for i = 1,len do
			local value = Ranking.get_ranking_item_value(list[i])
			if value < minActorLevel then
				len = i - 1
				break
			else
				totalValue = totalValue + value
			end
		end
		if len < minActorNum and Ranking.get_ranking_item_value(list[1]) > minLevel then
		 	minLevel = Ranking.get_ranking_item_value(list[1])
		else
			minLevel = math.floor(totalValue/minActorNum)
			if minLevel < minWorldLevel then
				minLevel = minWorldLevel
			end
		end
	end

	if curWorldLevel >= minLevel then
		return
	end

	--print("minLevel="..minLevel)
	System.setCultureLevel(minLevel)


	local players = LuaHelp.getAllActorList();
	if players then	
		for k, v in ipairs(players) do	
			--更新在线玩家的世界等级
			updateWorldCultureBuff(v)
		end
	end
end

function updateWorldCultureCircle()

	local minWorldCircle = WordCultureConfig.minWorldCircle
	local maxWorldCircle = WordCultureConfig.maxWorldCircle
	local minActorCircle = WordCultureConfig.minActorCircle
	local calcActorNum = WordCultureConfig.calcCircleNeedActorNum
	local calcMinActorNum = WordCultureConfig.calcCircleMinActorNum


	local curWorldCircle = System.getCultureCircle()
	local minCircle = minWorldCircle
	local list = LuaHelp.getActorCircleTopPlayer()
	local len = 0
	if list then
		len = #list
	end
	if len ~= 0 then
		if len > calcActorNum then
			len = calcActorNum
		end
		local totalValue = 0
		for i = 1,len do
			local value = Ranking.get_ranking_item_value(list[i])
			if value < minActorCircle then
				len = i - 1
				break
			else
				totalValue = totalValue + value
			end
		end
		if len < calcMinActorNum then
			len = calcMinActorNum
		end
		minCircle = math.floor(totalValue/len)
		if minCircle < minWorldCircle then
			minCircle = minWorldCircle
		elseif minCircle > maxWorldCircle then
			minCircle = maxWorldCircle
		end
	end

	if curWorldCircle >= minCircle then
		return
	end

	System.setCultureCircle(minCircle)
end

function FirstStartWorldCultureCircleCalc()
	local sVar = System.getStaticVar()
	if sVar.worldCultureCircleSpe then
		return
	end
	sVar.worldCultureCircleSpe = 1
	updateWorldCultureCircle()
end

---------------------------------------------世界等级更新 end------------------------------------------------

function updateWorldCultureBuff(sysarg)
	--上线判断世界等级的经验buff
	local nActorLevel = Actor.getActorTotalLevel(sysarg)
	local openDay = System.getDaysSinceOpenServer()
	local combineDay = System.getDaysSinceCombineServer()
	if nActorLevel >= WordCultureConfig.minStartLevel and openDay >= WordCultureConfig.OpenServerDay and (combineDay >= WordCultureConfig.CombineServerDay  or combineDay == 0 ) then
		local nCurMaxLevel = System.getCultureLevel()
		local levelDis
		for key ,value in ipairs(WordCultureConfig.CultureCfg) do
		 	if levelDis and levelDis > value.minLevel then
		 		levelDis = value.minLevel
		 	elseif not levelDis then
		 		levelDis = value.minLevel
		 	end
		end
		levelDis = levelDis - 1
		if nActorLevel >= nCurMaxLevel - levelDis then
			for key ,value in ipairs(WordCultureConfig.CultureCfg) do
			 	if Actor.hasBuffById(sysarg, value.buffId) then
			 		Actor.delBuffById(sysarg,value.buffId)
			 	end
			end
			--System.setCultureLevel(nActorLevel)
		else
		 	local nGapLevel = nCurMaxLevel-nActorLevel
		 	for key ,value in ipairs(WordCultureConfig.CultureCfg) do
		 		if nGapLevel >= value.minLevel and nGapLevel <= value.maxLevel then
		 			for k ,v in ipairs(WordCultureConfig.CultureCfg) do
		 				if Actor.hasBuffById(sysarg, v.buffId) then
		 					Actor.delBuffById(sysarg,v.buffId)
		 				end
		 			end
		 			--print("value.buffId="..value.buffId)
		 			Actor.addBuffById(sysarg,value.buffId)
	 				break
		 		end
		 	end
		end
	end
end
